//
//  WeatherMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "WeatherMessage.h"


#define PATH                      @"weather"

@implementation WeatherMessage

# pragma mark - notificationListen
- (void)notificationListen {
    __weak typeof(self)weakSelf = self;
//    //    15.4 通知事件ID 为固定的 3844
//    [weakSelf.deviceMgr onNotifyWeatherData:^(NSInteger errorCode, NSDictionary *uteDict) {
//        if(errorCode == UTEDeviceErrorNil) {
//            NSDictionary *data = [[NSDictionary alloc] initWithObjectsAndKeys:
//                                  PATH, KEY_PATH,
//                                  M_setFutureWeather, KEY_METHOD,
//                                  [uteDict valueForKey:@"data"], KEY_VALUE, nil];
//
//            [weakSelf.eventHander publishEvent:data async:YES];
//        }
//    }];
}

# pragma mark - methodCallConsumed
- (BOOL) methodCallConsumed:(NSString*)method params:(NSDictionary*)params result:(FlutterResult)result
{
    BOOL consumed = YES;
    if ([super methodCallConsumed:method params:params result:result])
        return YES;
    
    assert(method != nil);
    
    __weak typeof(self)weakSelf = self;
    
    if ([method isEqualToString:M_setTodayWeather]) {
        UTEModelWeatherToday *todayWeather = [weakSelf todayWeather:params];
        [[UTEDeviceMgr sharedInstance] setTodayWeatherModel:todayWeather block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    } else if([method isEqualToString:M_setFutureWeather]) {
        NSArray <UTEModelWeatherHourForecast *>* hourWeatherList = [weakSelf hourWeatherList:params];
        NSArray <UTEModelWeatherWeekForecast *>* futureWeatherList = [weakSelf futureWeatherList:params];
        [[UTEDeviceMgr sharedInstance] setFutureWeatherModel:hourWeatherList weekForecastListModel:futureWeatherList block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    }
    else {
        consumed = NO;
    }
    
    return consumed;
}
 
- (NSString *)getPath {
    return PATH;
}

# pragma mark - get today weather list
- (UTEModelWeatherToday *)todayWeather:(NSDictionary*)params {
    NSDictionary *map = [params objectForKey:TYPE_MAP];
    UTEModelWeatherToday *today = [UTEModelWeatherToday new];
    
    if ([map objectForKey:@"cityName"]) {
        today.cityName =[map objectForKey: @"cityName"];
    }
    
    today.unit = [[map objectForKey: @"unit"] intValue];
    today.currentTemperature = [[map objectForKey: @"currentTemperature"] intValue];
    today.lowest = [[map objectForKey: @"lowest"] intValue];
    today.highest = [[map objectForKey: @"highest"] intValue];
    today.humidity = [[map objectForKey: @"hum"] stringValue];
    today.uv = [[map objectForKey: @"uv"] stringValue];
    today.phenomenon = [[map objectForKey: @"phenomenon"] intValue];
    today.pm25 = [[map objectForKey: @"pm25"] intValue];
    today.publishTime = [[NSDate date] timeIntervalSince1970];
    
    return today;
}

# pragma mark - get hour weather list
- (NSArray <UTEModelWeatherHourForecast *>*)hourWeatherList:(NSDictionary*)params {
    NSDictionary *map = [params objectForKey:TYPE_MAP];
    NSArray *hourList = [map objectForKey:@"hourWeatherList"];
    
    NSMutableArray <UTEModelWeatherHourForecast *>* arr = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in hourList) {
        UTEModelWeatherHourForecast *hour = [UTEModelWeatherHourForecast new];
        hour.phenomenon = [[dict objectForKey:@"phenomenon"] intValue];
        hour.temperature = [[dict objectForKey:@"temperature"] intValue];
        hour.timestamp = [[dict objectForKey:@"timestamp"] intValue];
        
        [arr addObject: hour];
    }
    
    return arr;
}

# pragma mark - get future weather list
- (NSArray <UTEModelWeatherWeekForecast *>*)futureWeatherList:(NSDictionary*)params {
    NSDictionary *map = [params objectForKey:TYPE_MAP];
    NSArray * dayList = [map objectForKey:@"dayWeatherList"];
    
    NSMutableArray <UTEModelWeatherWeekForecast *>* arr = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in dayList) {
        UTEModelWeatherWeekForecast *week = [[UTEModelWeatherWeekForecast alloc] init];
        week.phenomenon = [[dict objectForKey:@"phenomenon"] intValue];
        week.highestTemperature = [[dict objectForKey:@"highestTemperature"] intValue];
        week.lowestTemperature = [[dict objectForKey:@"lowestTemperature"] intValue];
        week.timestamp = [[dict objectForKey:@"timestamp"] intValue];
        
        [arr addObject: week];
    }
    
    return arr;
}

@end
